package com.example.myapplication9_5;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static final int LOGIN_REQUEST_CODE = 1;

    private TextView txt_user_info;
    private TextView txt_nickName;
    private TextView txt_msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt_user_info = findViewById(R.id.txt_user_info);
        txt_nickName = findViewById(R.id.txt_nickName);
        txt_msg = findViewById(R.id.txt_msg);
        Button btnLogin = findViewById(R.id.btn_to_login);

        btnLogin.setOnClickListener(v -> {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivityForResult(intent, LOGIN_REQUEST_CODE);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ImageView img = findViewById(R.id.imageView2);
        if (requestCode == LOGIN_REQUEST_CODE && resultCode == RESULT_OK) {
            if (data != null) {
                String username = data.getStringExtra("uName");
                String nickname = data.getStringExtra("uNickname");

                // 更新界面显示
                txt_user_info.setText("用户信息");
                txt_nickName.setText("昵称：" + nickname);
                txt_msg.setText("用户名：" + username);
                img.setImageResource(R.drawable.p3);
            }
        }
    }
}